package com.chinalwb.are.demo.slice;

import com.chinalwb.are.ListStateBean;
import com.chinalwb.are.OnItemClickListener;
import com.chinalwb.are.demo.ARE_ShowTextAbility;
import com.chinalwb.are.demo.DemoUtil;
import com.chinalwb.are.demo.ResourceTable;
import com.chinalwb.are.utils.*;
import com.chinalwb.are.styles.ListComponent;
import com.chinalwb.are.view.RichTextView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;

/**
 * ARE_FullBottomAbilitySlice
 *
 * @since 2021/06/22
 */
public class ARE_FullBottomAbilitySlice extends AbilitySlice implements OnItemClickListener {
    private ListComponent listComponent;
    private String color = "#000000";
    private int textSize = 18;

    @Override
    public void onStart(Intent intent) {
        DemoUtil.initStatus(getAbility());
        getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_are__full_bottom);
        RichTextView tvContent = FindUtils.findRichTextById(getAbility(), ResourceTable.Id_tv_content);
        CursorTextField edit = FindUtils.findCursorTextFieldById(getAbility(), ResourceTable.Id_cursor_text);
        TextDecorator.getInstance().init(edit, tvContent);
        listComponent = FindUtils.findListComponentById(getAbility(), ResourceTable.Id_list_bottom);
        initList();
        listComponent.setOnItemClickListener(this);
        findComponentById(ResourceTable.Id_showText).setClickedListener(component -> {
            Intent intents = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withBundleName("com.chinalwb.are.demo")
                    .withAbilityName(ARE_ShowTextAbility.class.getCanonicalName())
                    .build();
            intents.setOperation(operation);
            startAbility(intents);
        });
        findComponentById(ResourceTable.Id_save).setClickedListener(
                component -> DemoUtil.saveHtml(getAbility(), tvContent.getText()));
    }

    private void initList() {
        Integer[] icon = {
                com.chinalwb.are.ResourceTable.Media_emoji,
                com.chinalwb.are.ResourceTable.Media_fontsize,
                com.chinalwb.are.ResourceTable.Media_bold,
                com.chinalwb.are.ResourceTable.Media_italic,
                com.chinalwb.are.ResourceTable.Media_underline,
                com.chinalwb.are.ResourceTable.Media_strikethrough,
                com.chinalwb.are.ResourceTable.Media_hr,
                com.chinalwb.are.ResourceTable.Media_subscript,
                com.chinalwb.are.ResourceTable.Media_superscript,
                com.chinalwb.are.ResourceTable.Media_quote,
                com.chinalwb.are.ResourceTable.Media_foregroundcolor,
                com.chinalwb.are.ResourceTable.Media_background,
                com.chinalwb.are.ResourceTable.Media_link,
                com.chinalwb.are.ResourceTable.Media_listnumber,
                com.chinalwb.are.ResourceTable.Media_listbullet,
                com.chinalwb.are.ResourceTable.Media_alignleft,
                com.chinalwb.are.ResourceTable.Media_aligncenter,
                com.chinalwb.are.ResourceTable.Media_alignright,
                com.chinalwb.are.ResourceTable.Media_image,
                com.chinalwb.are.ResourceTable.Media_video
        };
        listComponent.setData(getContext(), ListUtils.getList(icon, 0, 1, 10));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void itemClick(ListStateBean listStateBean, int position) {
        switch (position) {
            case 1:
                TextStyleUtils.getInstance().setTextSize(getContext(),
                        color, listComponent, textSize, size -> textSize = size);
                break;
            case 2:
                // 字体加粗
                TextStyleUtils.getInstance().setTextBold(listStateBean);
                break;
            case 3:
                // 斜体
                TextStyleUtils.getInstance().setTextItalic(listStateBean);
                break;
            case 4:
                // 下划线
                TextStyleUtils.getInstance().setTextUnderLine(listStateBean);
                break;
            case 5:
                // 中划线
                TextStyleUtils.getInstance().setTextStrikeThrough(listStateBean);
                break;
            case 7:
                // 下标
                TextStyleUtils.getInstance().setTextSubscript(listStateBean);
                break;
            case 8:
                // 上标
                TextStyleUtils.getInstance().setTextSuperscript(listStateBean);
                break;
            case 10:
                // 颜色
                TextStyleUtils.getInstance().setTextColor(getContext(), textSize, listComponent,
                        color, colors -> color = colors);
                break;
            case 11:
                // 背景
                TextStyleUtils.getInstance().setTextBg(listStateBean);
                break;
            default:
                new ToastDialog(getContext()).setText("暂不支持此功能").show();
                break;
        }
    }


    @Override
    protected void onBackPressed() {
        super.onBackPressed();
        TextDecorator.getInstance().release();
    }
}
